<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                新增房源
            </el-header>
            <el-main>
                <el-form ref="addHromData" :rules="rules" :model="addHromData" label-width="115px"
                         label-position="left">
                    <el-form-item label="名称 :" prop="name">
                        <el-input v-model="addHromData.name" placeholder="请输入名称"></el-input>
                    </el-form-item>
                    <el-form-item label="房号 :" prop="code">
                        <el-input v-model="addHromData.code" placeholder="请输入房号"></el-input>
                    </el-form-item>
                    <el-form-item label="面积(㎡) :" prop="mea">
                        <el-input v-model="addHromData.mea" placeholder="请输入面积"></el-input>
                    </el-form-item>
                    <el-form-item label="单价(元) :" prop="upic">
                        <el-input v-model="addHromData.upic" placeholder="请输入单价"></el-input>
                    </el-form-item>
                    <el-form-item label="招商状态 : " prop="cbo">
                        <el-select v-model="addHromData.cbo" filterable placeholder="请选招商状态" style="width: 100%;">
                            <el-option
                                    v-for="inst in cboOptions"
                                    :key="inst.uuid"
                                    :label="inst.name"
                                    :value="inst.val">
                                {{inst.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="房源用途 : " prop="purp">
                        <el-select v-model="addHromData.purp" filterable placeholder="请选择房源用途" style="width: 100%;">
                            <el-option
                                    v-for="inst in purpOptions"
                                    :key="inst.uuid"
                                    :label="inst.name"
                                    :value="inst.val">
                                {{inst.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="装修类型 : " prop="trimType">
                        <el-select v-model="addHromData.trimType" filterable placeholder="请选择装修类型" style="width: 100%;">
                            <el-option
                                    v-for="inst in trimTypeOptions"
                                    :key="inst.uuid"
                                    :label="inst.name"
                                    :value="inst.val">
                                {{inst.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="计价单位 : " prop="punt">
                        <el-select v-model="addHromData.punt" filterable placeholder="请选择计价单位" style="width: 100%;">
                            <el-option
                                    v-for="inst in puntOptions"
                                    :key="inst.uuid"
                                    :label="inst.name"
                                    :value="inst.val">
                                {{inst.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        保存
                    </el-button>
                    <el-button @click="editOut">
                        取消
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../utils/webUtils.js';

    export default {
        name: "addHrom",
        data() {
            return {
                formAlignLeft: 'left',
                uuid: '',
                addHromData: {
                    name: '',
                    code: '',
                    mea: '',
                    upic: '',
                    cbo: '',
                    purp: '',
                    trimType: '',
                    punt: ''
                },
                cboOptions: [],
                purpOptions: [],
                trimTypeOptions: [],
                puntOptions: [],
                rules: {
                    name: [
                        {required: true, message: '房源名称不能为空', trigger: 'blur'},
                    ],
                    code: [
                        {required: true, message: '房号不能为空', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            }
        },
        mounted() {
            this.queryCbo();
            this.queryPurp();
            this.queryTrimType();
            this.queryPunt();
        },
        methods: {
            //根据代码获取招商参数
            queryCbo() {
                this.axios
                    .request({
                        method: "get",
                        url: "./kwp/kdi/para/code/OPE_CBO",
                        params: {}
                    })
                    .then(response => {
                        this.cboOptions = response.data.subParas;
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            //根据代码获取房源用途参数
            queryPurp() {
                this.axios
                    .request({
                        method: "get",
                        url: "./kwp/kdi/para/code/OPE_HROM_PURPOSE",
                        params: {}
                    })
                    .then(response => {
                        this.purpOptions = response.data.subParas;
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            //根据代码获取装修类型参数
            queryTrimType() {
                this.axios
                    .request({
                        method: "get",
                        url: "./kwp/kdi/para/code/OPE_HROM_TRIM",
                        params: {}
                    })
                    .then(response => {
                        this.trimTypeOptions = response.data.subParas;
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            //根据代码获取计价单位参数
            queryPunt() {
                this.axios
                    .request({
                        method: "get",
                        url: "./kwp/kdi/para/code/OPE_PUNIT",
                        params: {}
                    })
                    .then(response => {
                        this.puntOptions = response.data.subParas;
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            submit() {
                this.$refs['addHromData'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.addHromData = replaceSpacesObj(this.addHromData);
                    this.axios.request({
                        method: "post",
                        url: "./ope/kdi/hrom",
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: {
                            name: this.addHromData.name,
                            code: this.addHromData.code,
                            mea: this.addHromData.mea,
                            upic: this.addHromData.upic,
                            cbo: this.addHromData.cbo,
                            purp: this.addHromData.purp,
                            trimType: this.addHromData.trimType,
                            punt: this.addHromData.punt
                        }
                    }).then(() => {
                        this.endProcessing();
                    }).catch(error => {
                        console.log(error);
                        this.info = '房源新增失败';
                        this.infoShow = true;
                    });
                });
            },
            endProcessing() {
                this.info = this.info + '新增成功,两秒后关闭';
                this.infoShow = true;
                setTimeout(() => {
                    this.infoShow = false;
                    this.$emit("editState", false);
                    this.cleanHrom();
                    this.info = '';
                }, 2000);
            },
            cleanHrom() {
                this.addHromData = {
                    name: '',
                    code: '',
                    mea: '',
                    upic: '',
                    cbo: '',
                    purp: '',
                    trimType: '',
                    punt: ''
                };
            },
            editOut() {
                this.infoShow = false;
                this.cleanHrom();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 334px;
        height: 100%;
        z-index: 100;
    }


</style>